<?php

namespace App\Http\Controllers;

use App\Http\Requests\RepositoryRequest;
use App\Http\Requests\SearchRequest;
use App\Models\Repository;

class RepositoryController extends Controller
{
    public function __construct()
    {
        $this->authorizeResource(Repository::class, 'repository');
    }

    /**
     * Display a listing of the resource.
     *
     * @param Repository $repository
     * @return \Illuminate\Http\Response
     */
    public function index(Repository $repository)
    {
        return view('resources.repository.index')->with('repositories', $repository->indexQuery()->paginate());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('resources.repository.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param RepositoryRequest $request
     *
     * @param Repository $repository
     * @return \Illuminate\Http\Response
     */
    public function store(RepositoryRequest $request, Repository $repository)
    {

        $repository->fill($request->only(['title', 'type', 'introduction', 'url']));
        $repository->tags = json_decode($request->tags,true);
        $repository->save();

        return redirect()->route('repositories.show', ['id' => $repository->id]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Repository $repository
     *
     * @return \Illuminate\Http\Response
     */
    public function show(Repository $repository)
    {
        $repository->timestamps = false;
        $repository->increment('views');
        $repository->load([
            'user',
            'likesCounter',
            'sourceRepository',
            'sourceRepository.user',
        ])->loadCount(['forks','issues','pulls']);

        $currentChapters = $repository->currentChapters()->paginate(5);

        return view('resources.repository.show')->with('repository', $repository)->with('currentChapters', $currentChapters);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Repository $repository
     *
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function edit(Repository $repository)
    {
//        $this->authorize('repositories.update', $repository);
        return view('resources.repository.edit')->with(compact('repository'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param RepositoryRequest $request
     * @param  \App\Models\Repository $repository
     *
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function update(RepositoryRequest $request, Repository $repository)
    {
//        $this->authorize('repositories.update', $repository);
        $repository->fill($request->only(['title', 'type', 'introduction', 'url']));
        $repository->tags = json_decode($request->tags,true);
        $repository->save();

        return redirect()->route('repositories.show', ['repository' => $repository]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Repository $repository
     *
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function destroy(Repository $repository)
    {
//        $this->authorize('repositories.delete', $repository);
        $repository->delete();

        return redirect()->route('repositories.index');
    }

    public function download(Repository $repository)
    {
        $repository = $repository->load(['currentChapters']);

        return response()->streamDownload(function () use ($repository) {

            foreach ($repository->currentChapters as $chapter) {
                $content = $chapter->content_download;
                echo $chapter->title ? sprintf("=======\r\n%s\r\n-------\r\n%s\r\n=======\r\n\r\n", $chapter->title, $content) : sprintf("=======\r\n%s\r\n=======\r\n\r\n", $content);
            }
        }, $repository->title.'.txt');
    }

    public function like(Repository $repository)
    {

        $repository->toggleLikeBy();
    }

    public function search(SearchRequest $request, Repository $repository)
    {

        return view('resources.repository.index')->with('repositories', $repository->where('title', 'like', '%'.$request->keyword.'%')->indexQuery()->paginate()->appends(['keyword']));
    }
}
